/////////////////////////////////////////////////////////////////////
//	$file: $
//	$revision: $
//	$author: $
//	$datetime: $
//	first author: buggy

#include	<QHeaderView>
#include	<QLayout>
#include	<QDir>
#include	<QKeyEvent>
#include	<QThread>
#include	<QDebug>

#include	"SourceNavigatorApp.h"
#include	"SourceNavigator.h"
#include	"SourceTree.h"
#include	"StatusBar.h"


///////////////////////////////////////////////////////////
//	Source Navigator Widget
SourceNavigator::SourceNavigator(const QString& rootPath)
	:	QWidget()
{
	_modeNav	= true;
	_searchText	= "";
	_layout		= new QVBoxLayout();
	_tree		= new SourceTree(rootPath, this);
	_bar		= new StatusBar(this);

	_layout->setContentsMargins(0, 0, 0, 0);
	_layout->setSpacing(0);
	_layout->addWidget(_tree);
	_layout->addWidget(_bar);

	setLayout(_layout);
	//	setFont(QFont("Monaco", 14));

	_bar->Deactivate();
	_tree->setFocus();
}

SourceNavigator::~SourceNavigator()
{
	if (_layout)
		delete _layout;
	if (_bar)
		delete _bar;
	if (_tree)
		delete _tree;
}

bool SourceNavigator::IsValid() const
{
	if (NULL == _tree)
		return false;
	return _tree->IsValid();
}

QString SourceNavigator::RootName() const
{
	if (NULL == _tree)
		return QString();
	return _tree->RootName();
}

QString SourceNavigator::RootPath() const
{
	if (NULL == _tree)
		return QString();
	return _tree->RootPath();
}

void SourceNavigator::OpenTextEditor(const SourceTreeItem* src, int startPosition/*= 0*/)
{
	SourceNavigatorApp::OpenTextEditor(src->Id(), src->AbsolutePath(), startPosition);
}

void SourceNavigator::CloseTextEditor(const SourceTreeItem* src)
{
	SourceNavigatorApp::CloseTextEditor(src->Id(), src->AbsolutePath());
}

void SourceNavigator::SaveTextEditorFile(const SourceTreeItem* src)
{
	SourceNavigatorApp::SaveTextEditorFile(src->Id(), src->AbsolutePath());
}

void SourceNavigator::MoveTextEditorWindow(const SourceTreeItem* src, int posIndex)
{
	SourceNavigatorApp::MoveTextEditorWindow(src->Id(), src->AbsolutePath(), posIndex);
}

void SourceNavigator::CloseAllTextEditors()
{
	for (int i = 0; i < _tree->topLevelItemCount(); ++i)
		closeFiles(static_cast<SourceTreeItem*>(_tree->topLevelItem(i)));
}

void SourceNavigator::closeFiles(SourceTreeItem* src)
{
	for (int i = 0; i < src->childCount(); ++i)
		closeFiles(static_cast<SourceTreeItem*>(src->child(i)));

	if (src->CanOpen())
		CloseTextEditor(src);
}

void SourceNavigator::SaveTextEditorAllFiles()
{
	for (int i = 0; i < _tree->topLevelItemCount(); ++i)
		saveFiles(static_cast<SourceTreeItem*>(_tree->topLevelItem(i)));
}

void SourceNavigator::saveFiles(SourceTreeItem* src)
{
	for (int i = 0; i < src->childCount(); ++i)
		saveFiles(static_cast<SourceTreeItem*>(src->child(i)));

	if (src->CanOpen())
		SaveTextEditorFile(src);
}


void SourceNavigator::ExecuteShell(const QString& shellCommand/* = ""*/)
{
	SourceNavigatorApp::OpenShellWindow(shellCommand);
}


void SourceNavigator::BeginCommandInput(QString command)
{
	if (command.at(0) == ':')
	{
		doCommand(command.remove(0, 1));
	}
	else if (command.at(0) == '/')
	{
		UpdateSearch(command.remove(0, 1));
	}

	CancelCommandInput();
}

void SourceNavigator::CancelCommandInput()
{
	//UpdateSearch("");
	_modeNav	= true;
	_bar->Deactivate();
	_tree->setFocus();
}

void SourceNavigator::UpdateSearch(const QString& search)
{
	_searchText	= search;
	doSearch();
}


void SourceNavigator::doCommand(const QString& command)
{
	if ("0" == command)
	{
		_tree->GoTop();
	}
	else if ("$" == command)
	{
		_tree->GoBottom();
	}
	else if ("q!" == command)
	{
		qApp->quit();
	}
	else if ("q" == command)
	{
		CloseAllTextEditors();
		qApp->quit();
	}
	else if ("w" == command)
	{
		SaveTextEditorAllFiles();
	}
	else if ("wq" == command)
	{
		SaveTextEditorAllFiles();
		CloseAllTextEditors();
		qApp->quit();
	}
	else if ('!' == command[0])
	{
		if ("make" == command.mid(1, 5))
			SaveTextEditorAllFiles();
		ExecuteShell(command.mid(1));
	}
}

void SourceNavigator::doSearch()
{
}

void SourceNavigator::TransitMode(QKeyEvent* event)
{
	if ( ":" == event->text())
		_modeNav	= false;
	else if ( "/" == event->text())
		_modeNav	= false;
	else
		return;

	_bar->Activate(event->text());
}

void SourceNavigator::ForwardKeyEventToCommand(QKeyEvent* event)
{
	_bar->keyPressEvent(event);
}

void SourceNavigator::ForwardKeyEventToNavigation(QKeyEvent* event)
{
	_tree->keyPressEvent(event);
	_tree->setFocus();
}

